package edu.uwm.capstone.diasmart.types;

import java.text.DecimalFormat;
import java.text.ParseException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.TableRow;
import edu.uwm.capstone.diasmart.R;
import edu.uwm.capstone.diasmart.database.DatabaseHelper;
import edu.uwm.capstone.diasmart.database.Entry;
import edu.uwm.capstone.diasmart.database.SubType;

public class GlucoseType implements EditableEntryType {

	private static final String PATTERN_IMPERIAL = "###0";

	private static final String PATTERN_METRIC = "##0.0";
	public static final int UNIT_IMPERIAL = 1;
	public static final int UNIT_METRIC = 1;
	private DecimalFormat imperialFormatter = new DecimalFormat(
			PATTERN_IMPERIAL);
	private DecimalFormat metricFormatter = new DecimalFormat(PATTERN_METRIC);

	public static double convertImperialtoMetric(double paramDouble) {
		return paramDouble / 18.0D;
	}

	public static double convertMetrictoImperial(double paramDouble) {
		return 18.0D * paramDouble;
	}

	public static int getDisplayedUnit(EntryContext paramEntryContext) {
		String str = paramEntryContext.getContext().getString(
				R.string.glucose_unit_preference_key);
		if ("mgdl".equals(paramEntryContext.getPreferences().getString(str,
				"mmol")))
			return 1;
		return 0;
	}

	public static double getHA1C(EntryContext paramEntryContext,
			double paramDouble) {
		if (getDisplayedUnit(paramEntryContext) == 0)
			paramDouble = convertMetrictoImperial(paramDouble);
		return (46.700000000000003D + paramDouble) / 28.699999999999999D;
	}

	public static void setDisplayedUnit(EntryContext paramEntryContext,
			int paramInt) {
		String str = paramEntryContext.getContext().getString(
				R.string.glucose_unit_preference_key);
		SharedPreferences.Editor localEditor = paramEntryContext
				.getPreferences().edit();
		if (paramInt == 1) {
			localEditor.putString(str, "mgdl");
		} else {
			localEditor.putString(str, "mmol");
		}
		localEditor.commit();

	}

	public String getEditableValue(EntryContext paramEntryContext,
			double paramDouble) {
		return getFormattedValue(paramEntryContext, paramDouble).replace(
				this.metricFormatter.getDecimalFormatSymbols()
						.getDecimalSeparator(), '.');
	}

	public String getFormattedValue(EntryContext paramEntryContext,
			double paramDouble) {
		if (getDisplayedUnit(paramEntryContext) == 1)
			return this.imperialFormatter.format(paramDouble);
		return this.metricFormatter.format(paramDouble);
	}

	public String getMeasurementUnit(EntryContext paramEntryContext) {
		if (getDisplayedUnit(paramEntryContext) == 1)
			return paramEntryContext.getContext().getString(R.string.mgdl);
		return paramEntryContext.getContext().getString(R.string.mmol);
	}

	public String getMeasurementUnit(EntryContext paramEntryContext,
			SubType paramSubType) {
		if ((paramSubType == null) || (paramSubType.getUnit() == null))
			return getMeasurementUnit(paramEntryContext);
		return paramSubType.getUnit();
	}

	public int getType() {
		return 0;
	}

	@Override
	public String getKeyboardType() {
		return "preference";
	}

	@Override
	public TableRow[] getUserInterface(Activity paramActivity) {
		return EditableEntryTypeHelper.getStandardUserInterface(paramActivity,
				this, R.id.edtGlucoseValue, R.id.sprGlucoseSubType);

	}

	@Override
	public boolean handleOnActivityResult(Activity paramActivity,
			int paramInt1, int paramInt2, Intent paramIntent) {
		return EditableEntryTypeHelper.handleOnActivityResult(paramActivity,
				getType(), R.id.sprGlucoseSubType, paramInt1, paramInt2,
				paramIntent);
	}

	@Override
	public long save(DatabaseHelper paramDatabaseHelper, Entry paramEntry,
			View paramView) {
		return EditableEntryTypeHelper.saveStandard(paramDatabaseHelper, this,
				paramEntry, paramView, R.id.edtGlucoseValue,
				R.id.sprGlucoseSubType);

	}

	@Override
	public int validate(View paramView) {
		return EditableEntryTypeHelper.validateStandard(this, paramView,
				R.id.edtGlucoseValue, R.id.sprGlucoseSubType);
	}

	@Override
	public int validate(EntryContext paramEntryContext, String paramString) {
		return EditableEntryTypeHelper.validateStandard(
				paramEntryContext.getContext(), paramString);
	}

	public double toActualValue(EntryContext paramEntryContext, long paramLong) {
		double d = paramLong / 1000.0D;
		if (getDisplayedUnit(paramEntryContext) == 1)
			d = convertMetrictoImperial(d);
		return d;
	}

	public long toStoredValue(EntryContext paramEntryContext, double paramDouble) {
		double d = paramDouble * 1000.0D;
		if (getDisplayedUnit(paramEntryContext) == 1)
			d = convertImperialtoMetric(d);
		return (long) d;
	}

	public String toString() {
		return /*EntryTypeFactory.getTypeString(getType()) */ "Glucose" ;
	}

	public double toValue(EntryContext paramEntryContext, String paramString) {
		String str = paramString.replace('.', this.metricFormatter
				.getDecimalFormatSymbols().getDecimalSeparator());
		try {
			if (getDisplayedUnit(paramEntryContext) == 1)
				return this.imperialFormatter.parse(str).doubleValue();
			double d = this.metricFormatter.parse(str).doubleValue();
			return d;
		} catch (ParseException localParseException) {
		}
		return Double.parseDouble(str);
	}

}
